package com.didispace.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


import java.io.Serializable;
import java.util.Collection;



import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.service.UserService;
import com.domain.User;

import org.springframework.cloud.client.ServiceInstance;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@Controller
public class HelloController {
	
	
	@Autowired
	private UserService userServices;
	
	 @Autowired
	 private DiscoveryClient client;
	 
	 @RequestMapping("/hello")
	    public String index() {
		 
		 
		 String name="aa";
		 int age=40;
		 userServices.create(name, age);
		     
		 return "Hello World";
	    }
	 
    @ResponseBody
	    @RequestMapping("/hello1")
	    public String hello() {
    	    String name="aa";
    	    userServices.deleteByName(name);
    	    return "Hello World";
	    }
    
    
    @ResponseBody
    @RequestMapping("/hello3")
    public String hello3() {
	    String name="aa";
	    int count=userServices.getAllUsers();
	    
	    return "Hello World"+count;
    }
    
    
    
   
    @RequestMapping("/user1")
    String userInfo(Map<String,Object> model){
    	 
    	String message="hello spring boot";
    	
        model.put("time",new Date());
        model.put("message", message);
        return "aa";
    }

   
    
      
	    @RequestMapping("/")
	    public String index(ModelMap map) {
	        map.addAttribute("host", "http://blog.didispace.com");
	        return "index";
}
	   
	    @ResponseBody
	    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
	        public User getuser( ) throws IOException {
	            User bob=new User();
	            bob.setName("bob");
	            bob.setAge(5);
	            return bob;
	        }
	    
	    @ResponseBody
	    @RequestMapping(method = RequestMethod.GET, value = "/getuser3")
	        public User getuser2( ) throws IOException {
	            User bob=new User();
	            bob.setName("bob");
	            bob.setAge(5);
	            return bob;
	        }
	    
	    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
	    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
	        ServiceInstance instance = client.getLocalServiceInstance();
	        Integer r = a + b;
	       
	        return r;
	      
	    }
	    
	    @RequestMapping(value = "/usersadd" ,method = RequestMethod.GET)
	    
	     public ResponseEntity <List<Map<String,Object>>> usersadd(){
	    	
	    	List<Map<String,Object>> user=userServices.getUsers();
	    	
	    	
	    	return new ResponseEntity <List<Map<String,Object>>>(user,HttpStatus.OK);
	    	
	     }
	    
	    
	    @ResponseBody
	    @RequestMapping(value = "/getuser3s" ,method = RequestMethod.GET)
	    
         public ResponseEntity <Map<String,Object>> getuser3s(){
	    	
	    	Map<String,Object> user=userServices.getuser3s();
	    	
	    	
	    	user.toString();
	    	
	    	
	         Integer aa=new Integer(3300);
	         user.put("bb", aa);
	         
	         Double bb=new Double(2.33);
	         user.put("cc",bb);
	         
	         
	         
	      
	    	
	    	
	    	return new ResponseEntity <Map<String,Object>>(user,HttpStatus.OK);
	    	
	     }
	    
	    
	   
	   
	    @RequestMapping(value = "/getuser4s")
	    
	    public ResponseEntity<?> getuser4s()
	    
	    {

	    	List<Map<String,Object>> user=userServices.getUsers();
	    	
	    	
	    	return new ResponseEntity <List<Map<String,Object>>>(user,HttpStatus.OK);
	    }
	    
       @RequestMapping(value = "/getuser5s")
	    
	    public ResponseEntity<?> getuser5s()
	    
	    {
             
           List<String> listApp=new ArrayList<String>(2);
           
           listApp.add("guan");
           listApp.add("bao");
           String [] array=new String[listApp.size()];
           array=listApp.toArray(array);
           
           
           
           
	    	List user1=userServices.getuser4s();
	    	
	    	List user2=userServices.getuser5s();
	    
	    	/*
        String saa = null;
        Iterator<String> it = user1.iterator();
        while (it.hasNext()) {

            saa = it.next();
        }

        user2.add(saa);
        */
	    	
	    	
	    	
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	
	    	map.put("user1", user1);
			map.put("user2", user2);
			map.put("user3", array);
			map.put("user4", listApp);
			
				
			
			
	    	
	    	 return new ResponseEntity <Map<String,Object>>(map,HttpStatus.OK);
	    }
	    
       
       @ResponseBody
       @RequestMapping(value="putUser/{id}", method=RequestMethod.GET)
       public String putUser(@PathVariable long id) {
           
    	   User user=new User();
    	   user.setName("aa");
    	   user.setAge(1);
    	   
           return "success"+user.getName()+user.getAge();
       }
      
       
       @ResponseBody
       @RequestMapping(value="putUserdd/{id}", method=RequestMethod.POST)
       public String putUser(@RequestParam(value="categoryNum",required=true) String categoryNum,
    		   String bb) 
           
    	   
    	{	   
    	   
    	   User user=new User();
    	   user.setName("aa");
    	   user.setAge(2);
    	   
           return "success"+user.getName()+user.getAge()+categoryNum+bb;
       }
      
     
       @ResponseBody
       @RequestMapping(value = "/getMap")  
       public Map<String, Object> getThisMap(String msg) {  
           Map<String, Object> map = new HashMap<>();  
           map.put("Name", "LYW");  
           map.put("Sex", "大老爷们");  
           map.put("Message", msg);  
           return map;  
       }  
       
       @ResponseBody
       @RequestMapping(value = "/getUser/{name}/{age}")  
       public User getUser(@PathVariable String name, @PathVariable Integer age) {  
           User user = new User();  
           //user.setId(12);  
           user.setName(name); 
           user.setAge(age);
           //user.setDate(new Date());  
           //user.setSex(sex);  
           return user;  
       }  
       
       
       
      
          
           @RequestMapping("/hello33")
           public String hello33(){
              
              return "cc";
              
              
           }
              
              
              
              
           
           @RequestMapping(value = "/helloUi", method = RequestMethod.GET)
                  @ResponseBody
                  public String hello(@RequestParam String name) {
                      return "Hello " + name;
                  }
              
              
              
              
              
              
              
           
          
       }
     
	    
	    
	    
	    
	    
	    

